package com.example.photography.adapter.focus;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.photography.R;
import com.example.photography.bean.FocusBean;
import com.example.photography.util.Util;
import com.example.photography.view.MyListView;

import java.util.List;

public class FocusAdapter extends RecyclerView.Adapter{

    /**
     * 可能感兴趣的
     */
    public static final int INE = 0;

    /**
     * 正在关注的
     */
    public static final int FOC = 1;
    private Context context;
    //类型
    private int currentType = 0;
    private java.util.List<FocusBean.DataBean.RelationsBean> relationsBeans;
    private List<FocusBean.DataBean.FocussBean> focussBeans;
    /**
     * 初始化布局
     */
    private final LayoutInflater layoutInflater;
    public FocusAdapter(Context context,List<FocusBean.DataBean.RelationsBean> relationsBeans, List<FocusBean.DataBean.FocussBean> focussBeans) {
        this.context = context;
        this.relationsBeans = relationsBeans;
        this.focussBeans = focussBeans;
        layoutInflater = LayoutInflater.from(context);
    }
    /**
     * 相当于getView 创建viewHolder部分代码
     *
     * @param parent
     * @param viewType 当前的类型
     * @return
     */
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == INE) {
            return new IntViewHolder(context, layoutInflater.inflate(R.layout.item_focus_int, null));
        } else if (viewType == FOC) {
            return new Focused_ViewHolder(context, layoutInflater.inflate(R.layout.item_focus_ed, null));
        }
        return null;
    }
    /**
     * 得到类型
     *
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        switch (position) {
            case INE:
                currentType = INE;
                break;
            case FOC:
                currentType = FOC;
                break;
        }
        return currentType;
    }

    /***
     * 相当于getView中的绑定数据模块
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == INE) {
            IntViewHolder intViewHolder = (IntViewHolder) holder;
            intViewHolder.setData(relationsBeans);
        }else if(getItemViewType(position) == FOC){
            Focused_ViewHolder focused_viewHolder = (Focused_ViewHolder) holder;
            focused_viewHolder.setData(focussBeans);
        }
    }

    /**
     * 总共有多少个item类型
     *
     * @return
     */
    @Override
    public int getItemCount() {
        return 2;
    }
    class IntViewHolder extends RecyclerView.ViewHolder{
        private final Context context;
        private RecyclerView rc_lab;
        private TextView tv_more;
        IntViewHolder(Context context, View view) {
            super(view);
            this.context = context;
            rc_lab = view.findViewById(R.id.rc_lab);
            tv_more = view.findViewById(R.id.tv_more);
            /**
             * 设置为横向RecyclerView
             */
            //设置RecyerView保持固定大小，可优化性能
            //创建LinearLayoutManager
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
            //RecyclerView设置为横向滑动
            linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            //设置创建LinearLayoutManager到RecyclerView中
            rc_lab.setLayoutManager(linearLayoutManager);
            tv_more.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Util.showToast(context,"更多~");
                }
            });
        }

        public void setData(List<FocusBean.DataBean.RelationsBean> relationsBeans) {
           IntAdpter intAdpter = new IntAdpter(context,relationsBeans);
           rc_lab.setAdapter(intAdpter);
        }
    }

    class Focused_ViewHolder extends RecyclerView.ViewHolder{
        private final Context context;
        private MyListView lv_user;
        private TextView tv_nums;

        Focused_ViewHolder(Context context, View view) {
            super(view);
            this.context = context;
            tv_nums =view .findViewById(R.id.tv_nums);
            lv_user = view.findViewById(R.id.lv_user);
        }

        public void setData(List<FocusBean.DataBean.FocussBean> focussBeans) {
            tv_nums.setText(focussBeans.size()+"人");

            UserAdapter userAdapter = new UserAdapter(focussBeans, context) {
                @Override
                public void focus() {
                    Util.showToast(context,"关注成功");
                }
            };
            lv_user.setAdapter(userAdapter);
        }


    }
}
